package com.edu.app.service.impl.wechat;

import com.edu.app.model.wechat.WxErrorException;
import com.edu.app.service.sys.wechat.WeChatServiceExecutor;
import com.edu.app.service.sys.wechat.WechatTemplateMsgService;
import com.edu.app.util.http.DataType;

import org.springframework.stereotype.Service;

import java.util.TreeMap;

@Service
public class WechatTemplateMsgServiceImpl implements WechatTemplateMsgService {

    String SEND_TEMPMSG_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s";

    WeChatServiceExecutor executor;

    WechatTemplateMsgServiceImpl(WeChatServiceExecutor executor){
        this.executor = executor;
    }
    /**
     * 发送模板消息

     * @param data
     * @return 状态
     */
    @Override
    public String sendTemplate(String data) throws WxErrorException {

        //TemplateMsgResult templateMsgResult = null;
        TreeMap<String, Object> params = new TreeMap<String, Object>();
        String response = null;
        try  {
            params.put("content",data);
            response = executor.execute(SEND_TEMPMSG_URL,params,ExecutorType.POSTSYNC, DataType.JSON);
            //templateMsgResult = (TemplateMsgResult)JsonUtil.fromJson(response, TemplateMsgResult.class);
        }
        catch(WxErrorException e){
          throw e;
        }

        return response;
    }
}
